package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.model.*;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dao.LogDao;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @Description:    日志实现类
* @Author:         xy
* @CreateDate:     2019/8/13 14:49
* @UpdateUser:     xy
* @UpdateDate:     2019/8/13 14:49
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@Service
public class LogServiceImpl implements ILogService {
    @Autowired
    private LogDao logDao;

    @Autowired
    private RedisDao redisDao;


    @Override
    public void addErrorLog(HttpServletRequest request, String ErrorInfo, String ParamInfo) {
        Map<String, Object> valuesMap = new HashMap<>();
        String token = request.getHeader("token");
        if (redisDao.existsKey(token)) {
            UserInfo userInfo = JwtUtil.getUserInfoFromToken(token,redisDao);
            if (userInfo != null) {
                valuesMap.put("P_USER_ID", userInfo.getUserId());
            } else {
                valuesMap.put("P_USER_ID", "未知");
            }
        } else {
            valuesMap.put("P_USER_ID", "token过期");
        }
        String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        valuesMap.put("SYSDATE",nowTime);
        valuesMap.put("V_LOG_URL", request.getRequestURL().toString());//访问地址
        valuesMap.put("V_LOG_USER_IP", request.getRemoteAddr());//访问ip
        valuesMap.put("V_REQUEST_DATA", ParamInfo);
        valuesMap.put("V_ERROR_INFO", ErrorInfo);
        logDao.addErrorLog(valuesMap);
    }

    @Override
    public boolean addMenuLog(Menu menu, UserInfo userInfo, HttpServletRequest request) {
        Map<String, Object> valuesMap = new HashMap<>();
        String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        valuesMap.put("SYSDATE",nowTime);
        valuesMap.put("V_LOG_USER_IP", request.getRemoteAddr());//访问ip
        valuesMap.put("V_LOG_URL", request.getRequestURL().toString());//访问地址
        valuesMap.put("V_MENU_ID", menu.getMenuId());
        valuesMap.put("P_USER_ID", userInfo.getUserId());
        int num = logDao.addMenuLog(valuesMap);
        return num > 0 ? true : false;
    }

    @Override
    public void logAdd(String logData, String op, String resultCode, String resultData, UserInfo userInfo, HttpServletRequest request) {
        try {
            boolean hasUserInfo=true;
            if(userInfo==null){
                hasUserInfo=false;
            }
            else if(userInfo.getUserId()==null){
                hasUserInfo=false;
            }
            Map<String, Object> valuesMap = new HashMap<>();
            valuesMap.put("V_LOG_TYPE", op);
            valuesMap.put("P_USER_ID", (hasUserInfo ?userInfo.getUserId():""));
            valuesMap.put("V_LOG_USER_ID", (hasUserInfo ? userInfo.getUserId():""));
            valuesMap.put("V_LOG_P_TOKEN", "");
            valuesMap.put("V_LOG_USER_IP", (request == null ? "" : request.getRemoteAddr()));//访问ip
            valuesMap.put("V_LOG_TIMESTAMP", "");//时间戳
            valuesMap.put("V_LOG_OPERATION", op);
            valuesMap.put("V_LOG_P_CODE", "");
            valuesMap.put("V_LOG_SIGN", "");//签名值
            valuesMap.put("V_LOG_SYS_ID", "");
            valuesMap.put("V_LOG_USER_ID_SYS", "");
            valuesMap.put("V_LOG_URL", (request == null ? "" : request.getRequestURL().toString()));//访问地址
            valuesMap.put("V_LOG_DATA", logData);
            valuesMap.put("V_LOG_NOW_IP", "");
            valuesMap.put("V_LOG_RESULT_CODE", resultCode);
            //String json = JSON.toJSONString(result);
            valuesMap.put("V_LOG_RESULT_MSG", resultData);
            logDao.logAdd(valuesMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public PageInfo<LogUserLogin> listLoginLog(DataPageVO dataPage){
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<LogUserLogin> list = logDao.listLoginLog(dataPage);
        PageInfo<LogUserLogin> pageInfo = new PageInfo<>(list, dataPage.getRows());
        return pageInfo;
    }
    @Override
    public PageInfo<LogOperation> listUserLog(DataPageVO dataPage){
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<LogOperation> list = logDao.listUserLog(dataPage);
        PageInfo<LogOperation> pageInfo = new PageInfo<>(list, dataPage.getRows());
        return pageInfo;
    }

    @Override
    public List<Map<String,Object>> getMenuChartsData(Map<String,Object> map){
//        int type=(int)map.get("time");
//        Calendar cal = Calendar.getInstance();
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        switch (type){
//            //今天
//            case 0:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//                //近7天
//            case 1:
//                cal.add(Calendar.DAY_OF_MONTH,-7);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//                //近30天
//            case 2:
//                cal.add(Calendar.DAY_OF_MONTH,-30);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            default:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//        }
        map.put("beginDate", map.get("beginDate") + " 00:00:00");
        map.put("endDate", map.get("endDate") + " 23:59:59");
        return logDao.getMenuChartsData(map);
    }

    @Override
    public List<Map<String,Object>> getUserChartsData(Map<String,Object> map){
//        int type=(int)map.get("time");
//        Calendar cal = Calendar.getInstance();
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        switch (type){
//            //今天
//            case 0:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            //近7天
//            case 1:
//                cal.add(Calendar.DAY_OF_MONTH,-7);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            //近30天
//            case 2:
//                cal.add(Calendar.DAY_OF_MONTH,-30);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            default:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//        }
        map.put("beginDate", map.get("beginDate") + " 00:00:00");
        map.put("endDate", map.get("endDate") + " 23:59:59");
        return logDao.getUserChartsData(map);
    }
    @Override
    public List<Map<String,Object>> getErrorChartsData(Map<String,Object> map){
//        int type=(int)map.get("time");
//        Calendar cal = Calendar.getInstance();
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        switch (type){
//            //今天
//            case 0:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            //近7天
//            case 1:
//                cal.add(Calendar.DAY_OF_MONTH,-7);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            //近30天
//            case 2:
//                cal.add(Calendar.DAY_OF_MONTH,-30);
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//            default:
//                map.put("beginDate",simpleDateFormat.format(cal.getTime()));
//                break;
//        }
        map.put("beginDate", map.get("beginDate") + " 00:00:00");
        map.put("endDate", map.get("endDate") + " 23:59:59");
        return logDao.getErrorChartsData(map);
    }
    @Override
    public  PageInfo<ErrorLog> listErrorLog(DataPageVO dataPage){
        PageHelper.startPage(dataPage.getPage(), dataPage.getRows());
        List<ErrorLog> list = logDao.listErrorLog(dataPage);
        return new PageInfo<>(list, dataPage.getRows());
    }
}